package com.sjh.sjhliving.commodity.controller;

import java.util.Arrays;
import java.util.Map;

//import org.apache.shiro.authz.annotation.RequiresPermissions;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.sjh.sjhliving.commodity.entity.AttrAttrgroupRelationEntity;
import com.sjh.sjhliving.commodity.service.AttrAttrgroupRelationService;
import com.sjh.sjhliving.commodity.service.CategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.sjh.sjhliving.commodity.entity.AttrEntity;
import com.sjh.sjhliving.commodity.service.AttrService;
import com.sjh.common.utils.PageUtils;
import com.sjh.common.utils.R;



/**
 * 商品属性表
 *
 * @author sjh
 * @email sjh@gmail.com
 * @date 2023-05-20 12:37:57
 */
@RestController
@RequestMapping("commodity/attr")
public class AttrController {
    @Autowired
    private AttrService attrService;

    @Autowired
    private CategoryService categoryService;

    @Autowired
    private AttrAttrgroupRelationService attrAttrgroupRelationService;

    /**
     * 列表
     */
    @RequestMapping("/list")
//    @RequiresPermissions("commodity:attr:list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = attrService.queryPage(params);

        return R.ok().put("page", page);
    }

    /**
     * 这里返回商品基本属性信息, 根据 categoryId+分页+查询添加
     * @param params
     * @param categoryId
     * @return
     */
    @GetMapping("/base/list/{categoryId}")
    public R baseAttrList(@RequestParam Map<String, Object> params, @PathVariable("categoryId") Long categoryId){
        PageUtils page = attrService.queryBaseAttrPage(params,categoryId);
        return R.ok().put("page", page);
    }

    /**
     * 这里返回商品销售属性信息, 根据 categoryId+分页+查询添加
     * @param params
     * @param categoryId
     * @return
     */
    @GetMapping("/sale/list/{categoryId}")
    public R saleAttrList(@RequestParam Map<String, Object> params, @PathVariable("categoryId") Long categoryId){

        PageUtils page = attrService.querySaleAttrPage(params,categoryId);
        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{attrId}")
//    @RequiresPermissions("commodity:attr:info")
    public R info(@PathVariable("attrId") Long attrId){
		AttrEntity attr = attrService.getById(attrId);

        return R.ok().put("attr", attr);
    }

    // 查询基本属性的基本值和 CascadedCategoryId
    @GetMapping("base/info/{attrId}")
    public R infoBaseAttr(@PathVariable("attrId") Long attrId){

        AttrEntity attr = attrService.getOne(new QueryWrapper<AttrEntity>().eq("attr_id", attrId));
        Long categoryId = attr.getCategoryId();
        Long[] cascadedCategoryId = categoryService.getCascadedCategoryId(categoryId);
        attr.setCascadedCategoryId(cascadedCategoryId);
        AttrAttrgroupRelationEntity attrAttrgroupRelationEntity = attrAttrgroupRelationService.getAttrAttrGroupRelationByAttr_id(attrId);
        if(attrAttrgroupRelationEntity != null){
            attr.setAttrGroupId(attrAttrgroupRelationEntity.getAttrGroupId());
        }
        return R.ok().put("attr", attr);
    }

    // 查询基本属性的基本值和 CascadedCategoryId
    @GetMapping("sale/info/{attrId}")
    public R infoSaleAttr(@PathVariable("attrId") Long attrId){

        AttrEntity attr = attrService.getOne(new QueryWrapper<AttrEntity>().eq("attr_id", attrId));
        Long categoryId = attr.getCategoryId();
        Long[] cascadedCategoryId = categoryService.getCascadedCategoryId(categoryId);
        attr.setCascadedCategoryId(cascadedCategoryId);
        return R.ok().put("attr", attr);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
//    @RequiresPermissions("commodity:attr:save")
    public R save(@RequestBody AttrEntity attr){
//		attrService.save(attr);
        attrService.saveAttrAndRelation(attr); // 添加属性基本信息，属性属性值关系
        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
//    @RequiresPermissions("commodity:attr:update")
    public R update(@RequestBody AttrEntity attr){
//		attrService.updateById(attr);
        attrService.updateAttrAndRelation(attr);
        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
//    @RequiresPermissions("commodity:attr:delete")
    public R delete(@RequestBody Long[] attrIds){
//		attrService.removeByIds(Arrays.asList(attrIds));
        attrService.delAttrAndRelation(Arrays.asList(attrIds));
        return R.ok();
    }

    /**
     * 删除销售属性， 不需要删除与属性组的关系，因为没有绑定
     * @param attrIds
     * @return
     */
    @RequestMapping("/sale/delete")
//    @RequiresPermissions("commodity:attr:delete")
    public R saleDelete(@RequestBody Long[] attrIds){
		attrService.removeByIds(Arrays.asList(attrIds));
        return R.ok();
    }

}
